﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract Catalogo typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class CatalogoDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<CatalogoDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Catalogo _currentCatalogo = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxCatalogoDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxCatalogoErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxCatalogoBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IdCatalogo property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdCatalogoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Isbn property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIsbnDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the NombreLibro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNombreLibroDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IdProveedor property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIdProveedorDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Estado property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxEstadoDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Proveedores> _IdProveedorList;
		
		/// <summary> 
		/// The list of selectable Proveedores
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Proveedores> IdProveedorList
		{
			get {return this._IdProveedorList;}
			set 
			{
				this._IdProveedorList = value;
				this.uxIdProveedorDataGridViewColumn.DataSource = null;
				this.uxIdProveedorDataGridViewColumn.DataSource = this._IdProveedorList;
			}
		}
		
		private bool _allowNewItemInIdProveedorList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Proveedores
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIdProveedorList
		{
			get { return _allowNewItemInIdProveedorList;}
			set
			{
				this._allowNewItemInIdProveedorList = value;
				this.uxIdProveedorDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Catalogo> _CatalogoList;
				
		/// <summary> 
		/// The list of Catalogo to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Catalogo> CatalogoList
		{
			get {return this._CatalogoList;}
			set
			{
				this._CatalogoList = value;
				this.uxCatalogoBindingSource.DataSource = null;
				this.uxCatalogoBindingSource.DataSource = value;
				this.uxCatalogoDataGridView.DataSource = null;
				this.uxCatalogoDataGridView.DataSource = this.uxCatalogoBindingSource;				
				//this.uxCatalogoBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxCatalogoBindingSource_ListChanged);
				this.uxCatalogoBindingSource.CurrentItemChanged += new System.EventHandler(OnCatalogoBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnCatalogoBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentCatalogo = uxCatalogoBindingSource.Current as Entities.Catalogo;
			
			if (_currentCatalogo != null)
			{
				_currentCatalogo.Validate();
			}
			//_Catalogo.Validate();
			OnCurrentEntityChanged();
		}

		//void uxCatalogoBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Catalogo"/> instance.
		/// </summary>
		public Entities.Catalogo SelectedCatalogo
		{
			get {return this._currentCatalogo;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxCatalogoDataGridView.VirtualMode;}
			set
			{
				this.uxCatalogoDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxCatalogoDataGridView.AllowUserToAddRows;}
			set {this.uxCatalogoDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxCatalogoDataGridView.AllowUserToDeleteRows;}
			set {this.uxCatalogoDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxCatalogoDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxCatalogoDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="CatalogoDataGridViewBase"/> class.
		/// </summary>
		public CatalogoDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxCatalogoDataGridView = new System.Windows.Forms.DataGridView();
			this.uxCatalogoBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxCatalogoErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdCatalogoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIsbnDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNombreLibroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIdProveedorDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxEstadoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIdProveedorBindingSource = new ProveedoresBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdProveedorBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCatalogoDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCatalogoBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCatalogoErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxCatalogoErrorProvider
			// 
			this.uxCatalogoErrorProvider.ContainerControl = this;
			this.uxCatalogoErrorProvider.DataSource = this.uxCatalogoBindingSource;						
			// 
			// uxCatalogoDataGridView
			// 
			this.uxCatalogoDataGridView.AutoGenerateColumns = false;
			this.uxCatalogoDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxCatalogoDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdCatalogoDataGridViewColumn,
		this.uxIsbnDataGridViewColumn,
		this.uxNombreLibroDataGridViewColumn,
		this.uxIdProveedorDataGridViewColumn,
		this.uxEstadoDataGridViewColumn			});
			this.uxCatalogoDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxCatalogoDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxCatalogoDataGridView.Name = "uxCatalogoDataGridView";
			this.uxCatalogoDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxCatalogoDataGridView.TabIndex = 0;	
			this.uxCatalogoDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxCatalogoDataGridView.EnableHeadersVisualStyles = false;
			this.uxCatalogoDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnCatalogoDataGridViewDataError);
			this.uxCatalogoDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnCatalogoDataGridViewCellValueNeeded);
			this.uxCatalogoDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnCatalogoDataGridViewCellValuePushed);
			
			//
			// uxIdCatalogoDataGridViewColumn
			//
			this.uxIdCatalogoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdCatalogoDataGridViewColumn.DataPropertyName = "IdCatalogo";
			this.uxIdCatalogoDataGridViewColumn.HeaderText = "IdCatalogo";
			this.uxIdCatalogoDataGridViewColumn.Name = "uxIdCatalogoDataGridViewColumn";
			this.uxIdCatalogoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdCatalogoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdCatalogoDataGridViewColumn.ReadOnly = true;		
			//
			// uxIsbnDataGridViewColumn
			//
			this.uxIsbnDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIsbnDataGridViewColumn.DataPropertyName = "Isbn";
			this.uxIsbnDataGridViewColumn.HeaderText = "Isbn";
			this.uxIsbnDataGridViewColumn.Name = "uxIsbnDataGridViewColumn";
			this.uxIsbnDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIsbnDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIsbnDataGridViewColumn.ReadOnly = false;		
			//
			// uxNombreLibroDataGridViewColumn
			//
			this.uxNombreLibroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNombreLibroDataGridViewColumn.DataPropertyName = "NombreLibro";
			this.uxNombreLibroDataGridViewColumn.HeaderText = "NombreLibro";
			this.uxNombreLibroDataGridViewColumn.Name = "uxNombreLibroDataGridViewColumn";
			this.uxNombreLibroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNombreLibroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNombreLibroDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdProveedorDataGridViewColumn
			//
			this.uxIdProveedorDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdProveedorDataGridViewColumn.DataPropertyName = "IdProveedor";
			this.uxIdProveedorDataGridViewColumn.HeaderText = "IdProveedor";
			this.uxIdProveedorDataGridViewColumn.Name = "uxIdProveedorDataGridViewColumn";
			this.uxIdProveedorDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdProveedorDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdProveedorDataGridViewColumn.ReadOnly = false;		
			//
			// uxEstadoDataGridViewColumn
			//
			this.uxEstadoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxEstadoDataGridViewColumn.DataPropertyName = "Estado";
			this.uxEstadoDataGridViewColumn.HeaderText = "Estado";
			this.uxEstadoDataGridViewColumn.Name = "uxEstadoDataGridViewColumn";
			this.uxEstadoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxEstadoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxEstadoDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdProveedorDataGridViewColumn
			//				
			this.uxIdProveedorDataGridViewColumn.DisplayMember = "NombreProveedor";	
			this.uxIdProveedorDataGridViewColumn.ValueMember = "IdProveedor";	
			this.uxIdProveedorDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIdProveedorDataGridViewColumn.DataSource = uxIdProveedorBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxCatalogoDataGridView);
			this.Name = "CatalogoDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdProveedorBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCatalogoErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCatalogoDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCatalogoBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				CatalogoDataGridViewEventArgs args = new CatalogoDataGridViewEventArgs();
				args.Catalogo = _currentCatalogo;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class CatalogoDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Catalogo	_Catalogo;
	
			/// <summary>
			/// the  Entities.Catalogo instance.
			/// </summary>
			public Entities.Catalogo Catalogo
			{
				get { return _Catalogo; }
				set { _Catalogo = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxCatalogoDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnCatalogoDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxCatalogoDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnCatalogoDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxCatalogoDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdCatalogoDataGridViewColumn":
						e.Value = CatalogoList[e.RowIndex].IdCatalogo;
						break;
					case "uxIsbnDataGridViewColumn":
						e.Value = CatalogoList[e.RowIndex].Isbn;
						break;
					case "uxNombreLibroDataGridViewColumn":
						e.Value = CatalogoList[e.RowIndex].NombreLibro;
						break;
					case "uxIdProveedorDataGridViewColumn":
						e.Value = CatalogoList[e.RowIndex].IdProveedor;
						break;
					case "uxEstadoDataGridViewColumn":
						e.Value = CatalogoList[e.RowIndex].Estado;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxCatalogoDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnCatalogoDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxCatalogoDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdCatalogoDataGridViewColumn":
						CatalogoList[e.RowIndex].IdCatalogo = (System.Int32)e.Value;
						break;
					case "uxIsbnDataGridViewColumn":
						CatalogoList[e.RowIndex].Isbn = (System.String)e.Value;
						break;
					case "uxNombreLibroDataGridViewColumn":
						CatalogoList[e.RowIndex].NombreLibro = (System.String)e.Value;
						break;
					case "uxIdProveedorDataGridViewColumn":
						CatalogoList[e.RowIndex].IdProveedor = (System.Int32)e.Value;
						break;
					case "uxEstadoDataGridViewColumn":
						CatalogoList[e.RowIndex].Estado = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
